//
//  YZMVistorView.swift
//  YZMweibo
//
//  Created by Simon on 16/4/18.
//  Copyright © 2016年 Simon. All rights reserved.
//

import UIKit
import SnapKit

class YZMVistorView: UIView {
    //定义一个闭包
    var visitorClosure: (() -> ())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - 外界调用方法
    func setVisitorInfo(imgName: String?, title: String?){
        
        // 消息 发现 我
        if let img = imgName, tit = title{
            iconImage.image = UIImage(named: img)
            messageLabel.text = tit
            // 隐藏圆圈
            circleImage.hidden = true
        }else{
            // 代表首页
            setCirCleAnim()
        }
        
    }
        // 叫圆圈转起来
    private func setCirCleAnim(){
        // 定义一个anim
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置动画执行事件
        anim.duration = 15
        // 设置toValue
        anim.toValue = 2 * M_PI
        // 动画次数
        anim.repeatCount = MAXFLOAT
        // 当我们切换tabbar 或者 程序退到后台 默认动画被移除
        anim.removedOnCompletion = false
        
        circleImage.layer.addAnimation(anim, forKey: nil)
    }
    
    // MARK: - 监听事件
    @objc private func loginClick(){
        visitorClosure?()
    }
   


    // MARK:- 设置约束
    private func setUI(){
    
    self.backgroundColor = RGB(red: 237, green: 237, blue: 237)
        //添加
        addSubview(circleImage)
        addSubview(maskImageView)
        addSubview(iconImage)
        addSubview(messageLabel)
        addSubview(loginBtn)
        addSubview(regisBtn)
        
        // 02 添加约束
        // 01
        circleImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
            
        }
        // 02
        maskImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self)
        }
        // 03
        iconImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        // 04
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
            make.centerX.equalTo(maskImageView)
            make.height.equalTo(36)
            make.width.equalTo(230)
        }
        // 05
        regisBtn.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        // 06
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(regisBtn)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
    
    }
    
    // MARK:- 懒加载控件
    //圆圈
    private lazy var circleImage = UIImageView(image: UIImage(named:"visitordiscover_feed_image_smallicon"))
    
    
    //图标
    private lazy var iconImage = UIImageView(image: UIImage(named:"visitordiscover_feed_image_house"))
    //遮盖
    private lazy var maskImageView: UIImageView = {
        
        let img = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
        return img
        
    }()

    //显示文字
    private lazy var messageLabel: UILabel = {
    let textLabel = UILabel()
        //颜色
        textLabel.textColor = UIColor.grayColor()
        //对齐方式 换行 内容
        textLabel.font = UIFont.systemFontOfSize(14)
        textLabel.numberOfLines = 0
        textLabel.textAlignment = .Center
        textLabel.text = "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜"
        
        return textLabel
    }()
    //登录按钮
    private lazy var loginBtn: UIButton = {
        let button = UIButton()
        //点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpOutside)
        //图片
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        //title
        button.setTitle("登录", forState: UIControlState.Normal)
        //颜色
        button.setTitleColor(themColor, forState: UIControlState.Normal)
        //字号
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        return button
    }()
    //注册按钮
    private lazy var regisBtn: UIButton = {
        let button = UIButton()
        //点击事件
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        //button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpOutside)
        //图片
        button.setBackgroundImage(UIImage(named:"common_button_white_disable"), forState: UIControlState.Normal)
        //title
        button.setTitle("注册", forState: UIControlState.Normal)
        //颜色
        button.setTitleColor(themColor, forState: UIControlState.Normal)
        //字号
        button.titleLabel?.font = UIFont.systemFontOfSize(14)

        return button
    }()
}
extension YZMVistorView{

}